package com.benyq.networkprogramme.base.adapter

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

/**
 * @author benyq
 * @date 18-9-14 12:14
 * @E-mail： 1520063035@qq.com
 * @description
 */
abstract class BenyqBaseAdapter<T>(protected var mContext: Context, protected var layoutId: Int) : RecyclerView.Adapter<BenyqRVHolder>() {
    private var mOnItemClickListener: OnItemClickListener? = null
    private var mOnItemLongClickListener: OnItemLongClickListener? = null

    val datas = mutableListOf<T>()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BenyqRVHolder {
        val view = LayoutInflater.from(parent.context).inflate(layoutId, parent, false)
        return BenyqRVHolder(mContext, view)
    }

    override fun onBindViewHolder(holder: BenyqRVHolder, position: Int) {

        mOnItemClickListener?.run {
            holder.itemView.setOnClickListener { onItemClick(holder.itemView, position)}
        }

        mOnItemLongClickListener?.run {
            holder.itemView.setOnLongClickListener {
                onItemLongClick(holder.itemView, position)
            false
            }
        }
        convert(holder, position, datas[position])
    }

    override fun getItemCount(): Int {
        return datas.size
    }

    abstract fun convert(holder: BenyqRVHolder, position: Int, bean: T)

    fun addAll(list: List<T>): Boolean {
        val result = datas.addAll(list)
        notifyDataSetChanged()
        return result
    }

    fun addAll(position: Int, list: List<T>): Boolean {
        val result = datas.addAll(position, list)
        notifyDataSetChanged()
        return result
    }

    fun add(data: T) {
        datas.add(data)
        notifyDataSetChanged()
    }

    fun add(position: Int, data: T) {
        datas.add(position, data)
        notifyItemInserted(datas.size - 1)
    }

    fun clear() {
        datas.clear()
        notifyDataSetChanged()
    }

    operator fun contains(data: T): Boolean {
        return datas.contains(data)
    }

    fun addNewData(data: List<T>?) {
        datas.clear()
        data?.run {
            datas.addAll(this)
            notifyDataSetChanged()
        }
    }

    fun remove(data: T): Boolean {
        val result = datas.remove(data)
        notifyDataSetChanged()
        return result
    }

    fun remove(index: Int) {
        datas.removeAt(index)
        notifyDataSetChanged()
    }

    interface OnItemClickListener {
        /**
         * @param view     The view whihin the ItemView that was clicked
         * @param position The position of the view int the adapter
         */
        fun onItemClick(view: View, position: Int)

    }

    interface OnItemLongClickListener {
        /**
         * @param view     The view whihin the ItemView that was clicked
         * @param position The position of the view int the adapter
         */

        fun onItemLongClick(view: View, position: Int)
    }

    fun setOnItemClickListener(listener: OnItemClickListener) {
        this.mOnItemClickListener = listener
    }

    fun setOnItemLongClickListener(listener: OnItemLongClickListener) {
        this.mOnItemLongClickListener = listener
    }
}